<%--
  Created by IntelliJ IDEA.
  User: lixiaodong
  Date: 2018/4/4
  Time: 14:17
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<div id="role_dialog" class="easyui-layout" data-options="fit:true,border:false">
    <div data-options="region:'center'">
        <table id="role_menu_treegrid" data-options="fit:true,border:false"></table>
    </div>
    <div data-options="region:'south'" style="height: 50px;padding-top: 10px;">
        <div style="position: absolute; right: 20px">
            <a href="javascript:;" id="role_add_ok" class="easyui-linkbutton" data-options="iconCls:'fa fa-check'" style="margin-right: 10px">确 定</a>
            <a href="javascript:;" id="role_add_cancel" class="easyui-linkbutton" data-options="iconCls:'fa fa-times'">关 闭</a>
        </div>
    </div>
</div>

<script type="text/javascript">
    var _role_menu_js = {
        _qp:null,
        treegrid_init : function () {
            _role_menu_js._qp = $('#role_dialog').dialog('options').queryParams;
            var _columns = [[
                {field:'id',title:'ID',width:0,hidden:'true'},
                //{field:'ck',checkbox:true},
                {field:'name',title:'中文名',width:200},
                {field:'number',title:'序号',width:100},
                {field:'value',title:'英文名',width:180},
                {field:'state',title:'状态',width:100},
                {field:'introduce',title:'说明',width:400},
                {field:'grade',title:'级别',width:0,hidden:'true'}
            ]];
            var _url = bs.base_url+'menu/getAll.action';
            //bs.treegrid_init(_url,'role_menu_treegrid', null,_columns);
            $('#role_menu_treegrid').treegrid({
                url: _url,
                idField: 'id',
                treeField: 'name',
                singleSelect: false,
                selectOnCheck: false,
                checkOnSelect: false,
                nowrap: true,
                striped: true,
                columns: _columns,
                loadMsg: false,
                loadFilter:function (data,parentId) {
                    if (data[0] != null && data[0].rows != null){
                        for (var i = 0; i < data[0].rows.length; i++){
                            if (data[0].rows[i]._parentId == ""){
                                data[0].rows[i]._parentId = null;
                            }
                        }
                    }
                    return data[0];
                },
                onLoadSuccess:function (row, data) {
                    $(".tree-icon,.tree-file").removeClass("tree-icon tree-file");
                    $(".tree-icon,.tree-folder").removeClass("tree-icon tree-folder tree-folder-open tree-folder-closed");
                    var _rowsP = $("#role_index_dg_menu").datagrid("getRows");
                    for (var i = 0; i < _rowsP.length; i++){
                        $('#role_menu_treegrid').treegrid('select',_rowsP[i].id);
                    }
                }
            });
        },
        btn_init:function () {
            $('#role_add_ok').on('click',function () {
                var _rows = $('#role_menu_treegrid').treegrid('getSelections');
                var _value = '';
                if (_rows != null){
                    for (var i = 0; i < _rows.length; i++){
                        _value += _rows[i].id + '▁';
                    }
                }
                var _url = bs.base_url + 'role/setMenus.action?id='+_role_menu_js._qp.row.id + '&menuIds=' + _value.substring(0,_value.length-1);
                $.ajax({
                    url:_url,
                    type:"post",
                    dataType:"json",
                    success: function(data){
                        //var result = JSON.parse(data);
                        if(data[0].status=='success'){
                            $.messager.alert('成功', data[0].description);
                            $('#role_index_dg_menu').datagrid('reload');
                            $("#role_dialog").dialog('destroy');
                        }else{
                            $.messager.alert('失败', data[0].description);
                        }
                    },error :function (){
                        $.messager.alert('警告','请求失败，请检查网络连接');
                    }
                });
            });
            $('#role_add_cancel').on('click',function () {
                $("#role_dialog").dialog('destroy');
            });
        },
    }

    var role_menu_app = (function () {
        $(function () {
            _role_menu_js.treegrid_init();
            _role_menu_js.btn_init();
        });
    })();
</script>